package com.springboot.cloud.demos.ribbon.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.exception.SystemErrorType;
import com.springboot.cloud.demos.entity.form.MomentSupportForm;
import com.springboot.cloud.demos.entity.po.MomentSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@Service
public class MomentSupportService {

    @Autowired
    RestTemplate restTemplate;

    @SentinelResource(fallback = "saveFallback")
    public Result save(@RequestBody MomentSupportForm userLessonForm) {
        return restTemplate.postForEntity("http://producer/momentSupport", userLessonForm, Result.class).getBody();
    }

    @SentinelResource(fallback = "querySignUpListFallBack")
    public Result<MomentSupport> queryByUser(@RequestParam("userId") String userId, @RequestParam("type") Integer type, @RequestParam("momentId") String momentId) {
        return restTemplate.getForEntity("http://producer/momentSupport/queryByUser?userId={1}&type={2}&momentId={3}", Result.class, userId, type, momentId).getBody();
    }

    public Result saveFallback(@RequestBody MomentSupportForm userLessonForm) {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

    public Result querySignUpListFallBack(String momentId) {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

    @SentinelResource(fallback = "queryByUnitAndUserFallback")
    public Result delete(@RequestParam("id") String id) {
        restTemplate.delete("http://producer/momentSupport/{1}", id);
        return Result.success();
    }

    @SentinelResource(fallback = "querySignUpListFallBack")
    public Result querySignUpList(String momentId) {
        return restTemplate.getForEntity("http://producer/momentSupport/querySignUpList?momentId={1}", Result.class, momentId).getBody();

    }
}
